#include<iostream>
#include<vector>
#include<string>
#include<chrono>
#include<set>
#include<sw/redis++/redis++.h>
#include<thread>
#include"util.hpp"
using sw::redis::Redis;
using namespace std::chrono_literals;
using namespace std;

void test1(Redis& redis)
{
    std::cout<<"hset and hget"<<std::endl;
    redis.flushall();

    redis.hset("key","f1","111");
    redis.hset("key",make_pair("f2","222"));

    // 能够一次性插入多个 field-value键值对
    redis.hset("key",{
        make_pair("f3","333"),
        make_pair("f4","444"),
        make_pair("f5","555"),
        make_pair("f6","666")
    });

    std::vector<pair<string,string>> fields = {
        make_pair("f7","777"),
        make_pair("f8","888")
    };
    redis.hset("key",fields.begin(),fields.end());

    auto result = redis.hget("key","f1");
    if(result)
    {
        cout<<"result: "<<result.value()<<endl;
    }
    else
        cout<<"result 无效"<<endl;

}

void test2(Redis& redis)
{
    std::cout<<"hexists"<<std::endl;
    redis.flushall();

    redis.hset("key","f1","111");
    redis.hset("key","f2","222");
    redis.hset("key","f3","333");

    auto flag = redis.hexists("key","f4");
    cout<<"flag:"<<flag<<endl;
}

void test3(Redis& redis)
{
    std::cout<<"hdel"<<std::endl;
    redis.flushall();

    redis.hset("key","f1","111");
    redis.hset("key","f2","222");
    redis.hset("key","f3","333");

    long long result = redis.hdel("key","f1");
    cout<<"result:"<<result<<endl;

    result = redis.hdel("key",{"f2","f3"});
    cout<<"result:"<<result<<endl;

}
void test4(Redis& redis)
{
    std::cout<<"hkeys and hvals"<<std::endl;
    redis.flushall();

    redis.hset("key","f1","111");
    redis.hset("key","f2","222");
    redis.hset("key","f3","333");

    vector<string> fields;
    auto it_fileds = std::back_inserter(fields);
    redis.hkeys("key",it_fileds);
    printContainer(fields);

    vector<string> values;
    auto it_values = std::back_inserter(values);
    redis.hvals("key",it_values);
    printContainer(values);
}
void test5(Redis& redis)
{
    std::cout<<"hmget and hmset"<<std::endl;
    redis.flushall();

    redis.hmset("key",{
        make_pair("f1","111"),
        make_pair("f2","222"),
        make_pair("f3","333")}
    );

    vector<std::pair<string,string>> pairs = {
        make_pair("f4","444"),
        make_pair("f5","555"),
        make_pair("f6","666")
    };

    redis.hmset("key",pairs.begin(),pairs.end());

    vector<string> values;
    auto it = std::back_inserter(values);
    redis.hmget("key",{"f1","f2","f3","f4","f5","f6"},it);
    printContainer(values);


}
int main()
{
    Redis redis("tcp://127.0.0.1:6379");
    // test1(redis);
    // test2(redis);
    // test3(redis);
    // test4(redis);
    test5(redis);
    return 0;
}